package com.example.myweb.Controller.Auth;


import com.example.myweb.Common.ApiResponse;
import com.example.myweb.Util.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public ApiResponse<String> upload(MultipartFile file) throws Exception {
        //把文件的内容存储到本地磁盘中
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的
        //通过UUID后，文件下载下来的名字，就会像浏览器下载下来的一样，是一串很长的不规律的字符串
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//        file.transferTo(new File("C:\\Users\\wugenghao\\Desktop\\files\\"+filename)); //该路径为电脑真实存在路径，后续下载的文件将存储在此文件夹
        String url = AliOssUtil.uploadFile(filename,file.getInputStream());
        return ApiResponse.success(url, "上传成功");
    }
}
